/* Created By Micha Sherman, google workshop 2012
 * 
 */

package com.sporteamws;

import com.appengine.utils.SavingAndLoadingCenter;
import com.datatypes.Settings;
import com.sporteamws.R.id;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

public class Options extends Activity implements
		android.widget.RadioGroup.OnCheckedChangeListener {

	RadioGroup myColor = null;
	RadioGroup ghostColor = null;
	RadioGroup myTime = null;
	RadioGroup ghostTime = null;

	CheckBox ttsOn = null;
	CheckBox myStatus = null;
	CheckBox ghostStatus = null;
	CheckBox gpsStatus = null;
	CheckBox screenOn = null;

	Settings settings = null;
	LocationManager lm = null;
	boolean stopGPS = true;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/** setup **/
		setContentView(R.layout.options);
		myColor = (RadioGroup) findViewById(R.id.radioGroupMy);
		myTime = (RadioGroup) findViewById(R.id.radioGroupMy_time);
		ghostColor = (RadioGroup) findViewById(R.id.radioGroupGhost);
		ghostTime = (RadioGroup) findViewById(R.id.radioGroupGhost_time);

		ttsOn = (CheckBox) findViewById(R.id.TTSon);
		screenOn = (CheckBox) findViewById(R.id.screenOn);
		myStatus = (CheckBox) findViewById(R.id.myStatus);
		ghostStatus = (CheckBox) findViewById(R.id.ghostStatus);
		gpsStatus = (CheckBox) findViewById(R.id.gpsStatus);
		/**      **/
		setupTabHost();
		settings = SavingAndLoadingCenter.loadSettings(this);

		lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		if (settings.getTtsMyDeley() == 60)
			myTime.check(id.M1min);
		else if (settings.getTtsMyDeley() == 180)
			myTime.check(id.M3min);
		else
			myTime.check(id.M5min);

		if (settings.getTtsGhostDeley() == 60)
			ghostTime.check(id.G1min);
		else if (settings.getTtsGhostDeley() == 180)
			ghostTime.check(id.G3min);
		else
			ghostTime.check(id.G5min);

		if (settings.getMyTrackColor() == Color.BLUE)
			myColor.check(R.id.blueM);
		else if (settings.getMyTrackColor() == Color.BLACK)
			myColor.check(R.id.blackM);
		else if (settings.getMyTrackColor() == Color.RED)
			myColor.check(R.id.redM);
		else
			myColor.check(R.id.greeM);

		if (settings.getGhostTrackColor() == Color.BLUE)
			ghostColor.check(R.id.blueG);
		else if (settings.getGhostTrackColor() == Color.BLACK)
			ghostColor.check(R.id.blackG);
		else if (settings.getGhostTrackColor() == Color.RED)
			ghostColor.check(R.id.redG);
		else
			ghostColor.check(R.id.greeG);

		ttsOn.setChecked(settings.isTtsOn());
		screenOn.setChecked(settings.isScreenOn());
		gpsStatus.setChecked(settings.isTtsGpsStatus());
		ghostStatus.setChecked(settings.isTtsGhostStatus());
		myStatus.setChecked(settings.isTtsMyStatus());

		myColor.setOnCheckedChangeListener(this);
		ghostColor.setOnCheckedChangeListener(this);
		myTime.setOnCheckedChangeListener(this);
		ghostTime.setOnCheckedChangeListener(this);

		ttsOn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				settings.setTtsOn(isChecked);
			}
		});

		screenOn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				settings.setScreenOn(isChecked);
			}
		});

		myStatus.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				settings.setTtsMyStatus(isChecked);
			}
		});

		ghostStatus.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				settings.setTtsGhostStatus(isChecked);
			}
		});

		gpsStatus.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				settings.setTtsGpsStatus(isChecked);
			}
		});

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		SavingAndLoadingCenter.saveSettings(settings, this);
		if (stopGPS) {
			lm.removeUpdates(SharedPref.lb);
			SharedPref.onHome = true;
		}
		finish();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		stopGPS = false;
	}

	void setupTabHost() {
		TabHost OptionTabHost = (TabHost) findViewById(R.id.tabhost);
		OptionTabHost.setup();

		TabSpec specs = OptionTabHost.newTabSpec("tag general");
		specs.setContent(R.id.tab1);
		specs.setIndicator("general");
		OptionTabHost.addTab(specs);

		specs = OptionTabHost.newTabSpec("tag TTS");
		specs.setContent(R.id.tab2);
		specs.setIndicator("Audio info");
		OptionTabHost.addTab(specs);

	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {

		switch (checkedId) {
		case R.id.blueM:
			settings.setMyTrackColor(Color.BLUE);
			break;
		case R.id.blueG:
			settings.setGhostTrackColor(Color.BLUE);
			break;
		case R.id.blackM:
			settings.setMyTrackColor(Color.BLACK);
			break;
		case R.id.blackG:
			settings.setGhostTrackColor(Color.BLACK);
			break;
		case R.id.redM:
			settings.setMyTrackColor(Color.RED);
			break;
		case R.id.redG:
			settings.setGhostTrackColor(Color.RED);
			break;
		case R.id.greeM:
			settings.setMyTrackColor(Color.GREEN);
			break;
		case R.id.greeG:
			settings.setGhostTrackColor(Color.GREEN);
			break;

		case R.id.M1min:
			settings.setTtsMyDeley(60);
			break;
		case R.id.G1min:
			settings.setTtsGhostDeley(60);
			break;
		case R.id.M3min:
			settings.setTtsMyDeley(180);
			break;
		case R.id.G3min:
			settings.setTtsGhostDeley(180);
			break;
		case R.id.M5min:
			settings.setTtsMyDeley(300);
			break;
		case R.id.G5min:
			settings.setTtsGhostDeley(300);
			break;

		default:
			break;
		}

	}

}
